﻿/**
* 该类为滚动主程序类
* @author lbynet
* @version 0.1
*/

package com.xuanque.mahjongDice.ScrollBarui{
	import flash.display.*;
	import flash.events.*;

	public class ScrollBarY extends MovieClip {
		
		private var yOffset:Number;
		private var yMin:Number = 0;
		private var yMax:Number;
		private var speed:Number = 10;
		private var newheight:Number=0;
		
		public function ScrollBarY():void {
			
			//yMax = this.newHeight();
			//init();
			
		}
		
		public function init():void {
			
			this.newHeight();
			
			thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);

			up_btn.addEventListener(MouseEvent.MOUSE_DOWN, upHandler);
			down_btn.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			track.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
		}
		
		private function thumbDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_MOVE,thumbMove);
			yOffset=mouseY - thumb.y;
		}

		private function thumbUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,thumbMove);
		}

		private function thumbMove(e:MouseEvent):void {
			thumb.y=mouseY - yOffset;
			if (thumb.y <= yMin) {
				thumb.y=yMin;
			}
			if (thumb.y >= yMax) {
				thumb.y=yMax;
			}
			dispatch(e);
		}
		private function upHandler(e:MouseEvent):void {

			thumb.y -= this.speed;

			if (thumb.y <= yMin) {
				thumb.y=yMin;
			}
			dispatch(e);
		}
		private function downHandler(e:MouseEvent):void {

			thumb.y += this.speed;

			if (thumb.y >= yMax) {
				thumb.y=yMax;
			}
			dispatch(e);
		}
		private function clickHandler(e:MouseEvent):void {
			var _mc:MovieClip = e.target as MovieClip;
			if (_mc.mouseY > yMax) {
				thumb.y=yMax;
			} else {
				thumb.y = _mc.mouseY;
			}
			dispatch(e);
		}
		
		private function dispatch(e:MouseEvent) {
			
			dispatchEvent(new ScrollBarYEvent(thumb.y / yMax));
			e.updateAfterEvent();
		}
		
		public function newHeight(num:Number = 0):void {
			
			if (num == 0) {
				this.newheight = track.height - thumb.height;
			}else {
				thumb.height = num;
				this.newheight = track.height - num;
			}
			yMax = this.newheight
		}
		
		private function refresh() {
			
		}
		
		
		public function wheelUpHandler(wheelspeed:int):void {
			
			thumb.y -= wheelspeed;
			
			if (thumb.y <= yMin) {
				thumb.y=yMin;
			}
			dispatchEvent(new ScrollBarYEvent(thumb.y / yMax));
		}
		public function wheelDownHandler(wheelspeed:int):void {
			
			thumb.y += wheelspeed;
			
			if (thumb.y >= yMax) {
				thumb.y=yMax;
			}
			dispatchEvent(new ScrollBarYEvent(thumb.y / yMax));
		}
		
		
		
	}
}